{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6adfc61",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "> * 1.list的一般方法/函数的使用案例，如：\n",
    ">>1. append（）增加，增加list元素到list的末尾，常用于新建列表\n",
    ">>2.pop（index）/remove（values）对比两者优势\n",
    ">>3.count（）查看某个元素出现的次数\n",
    ">>4.index（）查看某个元素第一次出现的位置\n",
    ">>5.extend（）两个list合并在一起，也可用[]+[]来实现\n",
    ">>6. sort（）list元素的排序\n",
    "\n",
    ">* 2. list的高级用法：如\n",
    ">>1.copy（）list（可变的数据类型）复制 不等价于 赋值，可用[:]\n",
    ">>2. enumerate（）枚举，可同时遍历并获取index和values\n",
    "\n",
    "## 本周内容\n",
    "#### list练习，vowels.py vowels2.py\n",
    "#### list实践：\n",
    "#### 初始字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ef13749d",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"zhichao\"\n",
    "name = \"youge\"\n",
    "# 最后name是youge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "378b7cd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']# 这是元音(list)\n",
    "word = \"milliways\" #这是文本(str)\n",
    "# 检查 word 中出现的元音，并打印出来 怎么做？\n",
    "# for letter in word： # 第一个元素letter 请不要和已有变量重名\n",
    "                       # 第一个元素一般不定义名字，常用item（元素）来命名\n",
    "                       # 简写i来命名\n",
    "                       # 第二个元素word（重要），意味着将遍历的内容\n",
    "                       # 遍历的内容一般是 序列数据str（如 m是0，i是2...） list\n",
    "for letter in word: \n",
    "    if letter in vowels:\n",
    "        print(letter)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd8d7e7",
   "metadata": {},
   "source": [
    "* vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d1a17df8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'a']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用append () 新建列表\n",
    "found = []\n",
    "for letter in word:\n",
    "#      print(letter)\n",
    "    if letter in vowels: # letter 在 元音字母表中\n",
    "        if letter not in found: # letter 不在found表中\n",
    "            found.append(letter)\n",
    "found      "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f1385e8",
   "metadata": {},
   "source": [
    "#### list实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "724b7fe7",
   "metadata": {},
   "outputs": [],
   "source": [
    "购物车程序项目：\n",
    "\n",
    "要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　  2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　  3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a77e03d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面是我在网上借鉴滴："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dd36223",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "product_list = [\n",
    "    ('coffee',30),\n",
    "    ('bike',599),\n",
    "    ('book',49),\n",
    "    ('earphone',88),\n",
    "    ('pen',99),\n",
    "    ('computer',3499),\n",
    "]\n",
    " \n",
    "shopping_list=[]\n",
    "salary = input(\"please input your salary:\")\n",
    "if salary.isdigit():\n",
    "    salary = int(salary)\n",
    "    while True:\n",
    "        for index,item in enumerate(product_list):\n",
    "         #print(product_list.index(item),item)\n",
    "             print(index,item)\n",
    "        user_choice = input(\"请选择你好购买的商品：\")\n",
    "        if user_choice.isdigit():\n",
    "             user_choice = int(user_choice)\n",
    "             if user_choice < len(product_list) and user_choice >=0:\n",
    "                 p_item = product_list[user_choice]\n",
    "                 if p_item[1] <= salary:\n",
    "                    shopping_list.append(p_item)\n",
    "                    salary -= p_item[1]\n",
    "                    print(\"added %s into shopping cart,your balance is %s\"%(p_item,salary))\n",
    "                 else:\n",
    "                    print(\"你的余额只剩%s,无法购买\" % salary)\n",
    "             else:\n",
    "                print(\"商品不存在\")\n",
    "        elif user_choice == 'q':\n",
    "            print('--------shopping list----------')\n",
    "            for p in shopping_list:\n",
    "                print(p)\n",
    "            print(\"您的余额是：\",salary)\n",
    "            exit()\n",
    "        else:\n",
    "             print(\"您的salary  不足，无法购买，sorry\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c4f02eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 下面是老师上课讲滴："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b1ae306",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 商品信息的数据存放\n",
    "commodities list = [(\"apple\",10100),\n",
    "                    (\"steak\",13402),\n",
    "                    (\"toothbrush\",10233),\n",
    "                    (\"socks\",14155)]\n",
    "\n",
    "# 用户信息的数据存放\n",
    "shopping_list = []\n",
    "\n",
    "# 输入余额\n",
    "balance = input (\"input your balance\")\n",
    "# 商品信息的数据存放 (商品名称，商品价格，商品的编号)\n",
    "goods_list = [\n",
    "    [\"python课本\",60],  # 我们认为，每个商品的内容是一个多个值的值，可选用list\n",
    "    [\"华为手机\",8000],\n",
    "    [\"小米手环\",199],\n",
    "    [\"蜜雪冰城\",8]\n",
    "]\n",
    "# 用户购物信息的数据存放\n",
    "user_shopping_list = [] # 增加进来可考虑用append()方法\n",
    "# 用户输入余额信息\n",
    "Alipay = int(input(\"请输入支付宝的余额：\")\n",
    "# 打印商品信息\n",
    "# info = '''----- goods list -----\n",
    "# 商品编号: %s,商品名称: %s,商品价格: %s 元\n",
    "# 商品编号: %s,商品名称: %s,商品价格: %s 元\n",
    "# 商品编号: %s,商品名称: %s,商品价格: %s 元\n",
    "# 商品编号: %s,商品名称: %s,商品价格: %s 元\n",
    "# '''%(0,goods_list[0][0],goods_list[0][1],\n",
    "#      2,goods_list[1][0],goods_list[1][1],\n",
    "#      3,goods_list[2][0],goods_list[2][1],\n",
    "#      4,goods_list[3][0],goods_list[3][1],\n",
    "#  )\n",
    "# print(info)\n",
    "\n",
    "# list的长度？ len()\n",
    "             \n",
    "# 执行循环\n",
    "while True:\n",
    "   print('----- goods list -----')\n",
    "   for i in range(len(goods_list)):\n",
    "       print(\"商品编号: %s,商品名称: %s,商品价格: %s 元\"%(i,goods_list[i][0],goods_list[i]))\n",
    "\n",
    "  # 用户输入商品编号，实现商品的查询\n",
    "  choice = input(\"请输入商品的编号: \") # input输入的内容默认是字符串str 例如输入2，则显示\"2\"\n",
    "  if choice.isdigit          \n",
    "             \n",
    "  # 判断编号是否存在\n",
    "  if int(choice) < len(goods_list):\n",
    "     # 检查用户余额  > 商品价格\n",
    "     if Alipay > goods_list[choice][1]:\n",
    "        Alipay = Alipay - goods_list[choice][1] # 用户余额发生变化， 要扣除\n",
    "        user_shopping_list.append(goods_list[choice])\n",
    "     else:\n",
    "        print(\"您的余额已不足，请更换支付方式，试试花呗？\")\n",
    " else:\n",
    "    print(\"请输入正确的商品编号，您输入的商品不存在\")\n",
    "    \n",
    "     elif choice == \"退出\":\n",
    "\n",
    " # 打印用户余额和用户的商品列表信息\n",
    " print(\"用户余额: %s,购物车:\")\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a622a01",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
